//
//  ModifyViewController.m
//  OrderFood
//
//  Created by xiaofang wang on 2018/10/22.
//  Copyright © 2018年 zhiyuan. All rights reserved.
//

#import "ModifyViewController.h"
#import "LoginViewController.h" //12-29

@interface ModifyViewController ()
@property (nonatomic,strong) UITextField *oldpasswordTF; //旧密码
@property (nonatomic,strong) UITextField *newpasswordTF; //新密码
@property (nonatomic,strong) UITextField *sureNewpwTF;   //新确认密码
@property (nonatomic,strong) UIButton *btnGetCode;
@property (nonatomic,strong) UIButton *sureBtn;
@end

@implementation ModifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.name = @"修改登录密码";
    NSArray *placeAry = @[@"请输入旧密码",@"请设置新的登录密码",@"请确认新的登录密码"];
    for (int i = 0; i < placeAry.count; i++) {
        
        UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(15, 100+50*i, SCREEN_WIDTH-110, 50)];
        tf.placeholder = placeAry[i];
        [tf setValue:themeTextColor forKeyPath:@"_placeholderLabel.textColor"];
        [tf setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
        tf.secureTextEntry = YES;
        [self.view addSubview:tf];
        [QTTools tool_addLineWithFrame:CGRectMake(15, 150+50*i, SCREEN_WIDTH - 30, 1) superView:self.view];
        if (i == 0) {
            self.oldpasswordTF = tf;
            UIButton *seeBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
            seeBtn1.frame = CGRectMake(SCREEN_WIDTH-60, GetMinY(self.oldpasswordTF)+8, 44, 34);
            [seeBtn1 setImage:IMAGE(@"by") forState:UIControlStateNormal];
            [self.view addSubview:seeBtn1];
            [seeBtn1 addTarget:self action:@selector(seeAction1:) forControlEvents:UIControlEventTouchUpInside];
        }else if (i == 1){
            self.newpasswordTF = tf;
            UIButton *seeBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
            seeBtn2.frame = CGRectMake(SCREEN_WIDTH-60, GetMinY(self.newpasswordTF)+8, 44, 34);
            [seeBtn2 setImage:IMAGE(@"by") forState:UIControlStateNormal];
            [self.view addSubview:seeBtn2];
            seeBtn2.selected = NO;
            [seeBtn2 addTarget:self action:@selector(seeAction:) forControlEvents:UIControlEventTouchUpInside];
            
        }else if (i == 2){
            self.sureNewpwTF = tf;
//            UIButton *seeBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
//            seeBtn3.frame = CGRectMake(SCREEN_WIDTH-60, GetMinY(self.sureNewpwTF)+8, 44, 34);
//            [seeBtn3 setImage:IMAGE(@"by") forState:UIControlStateNormal];
//            [self.view addSubview:seeBtn3];
//            seeBtn3.selected = NO;
//            [seeBtn3 addTarget:self action:@selector(seeAction:) forControlEvents:UIControlEventTouchUpInside];
        }else{
//            self.sureNewpwTF = tf;
        }
    }
  
    _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureBtn.frame = CGRectMake(15, 100+50*4+35, SCREEN_WIDTH-30, 44);
    _sureBtn.backgroundColor = themeColor;
    _sureBtn.layer.cornerRadius = 5;
    _sureBtn.layer.masksToBounds = YES;
    _sureBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [_sureBtn setTitleColor:WHITECOLOR forState:UIControlStateNormal];
    [_sureBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];

    [_sureBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [_sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.sureBtn];
    
    
}

-(void)getCodeAction:(UIButton *)sender{
    
    
}
-(void)sureAction:(UIButton *)sender{
    
    NSString *oldStr = self.oldpasswordTF.text;
    NSString *newStr = self.newpasswordTF.text;
    NSString *sureStr = self.sureNewpwTF.text;
    
    //一 错误判断
    if ([NSString rb_isEmptyString:oldStr]) {
        [IndicatorManager showMessage:@"请输入旧密码"];
        return;
    }
    if ([NSString rb_isEmptyString:newStr]) {
        [IndicatorManager showMessage:@"请输入新密码"];
        return;
    }
    if ([NSString rb_isEmptyString:@"请输入确认密码"]) {
        [IndicatorManager showMessage:@"请输入新确认密码"];
        return;
    }
    if (![newStr isEqualToString:sureStr]) {
        [IndicatorManager showMessage:@"两次密码不一致"];
        return;
    }
    
    //二 调接口
    [self modifyPwdRquestWithOldStr:oldStr xinPwd:sureStr];
}


-(void)seeAction1:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setImage:IMAGE(@"zy") forState:UIControlStateNormal];
        self.oldpasswordTF.secureTextEntry = NO;
    }else{
        [sender setImage:IMAGE(@"by") forState:UIControlStateNormal];
        self.oldpasswordTF.secureTextEntry = YES;
    }
}

-(void)seeAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setImage:IMAGE(@"zy") forState:UIControlStateNormal];
        self.newpasswordTF.secureTextEntry = NO;
        self.sureNewpwTF.secureTextEntry = NO;
    }else{
        [sender setImage:IMAGE(@"by") forState:UIControlStateNormal];
        self.newpasswordTF.secureTextEntry = YES;
        self.sureNewpwTF.secureTextEntry = YES;
    }
}
#pragma mark - 网络请求
- (void)modifyPwdRquestWithOldStr:(NSString *)oldpwd xinPwd:(NSString *)xinpwd{
    
    [NetWork requestModifyPasswordWithOldPwd:oldpwd xinPwd:xinpwd successBlock:^(NSDictionary * _Nonnull response) {
        [IndicatorManager showMessage:@"修改成功"];
        if (RES_SUCESS) {//解析数据
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                //切换根视图控制
                //反射
                LoginViewController *loginVC = [[LoginViewController alloc] init];
                //清空密码页面
                loginVC.passwordTF.text = @"";
                loginVC.passwordTF.placeholder = @"请输入新密码";
                UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
                [UIApplication sharedApplication].keyWindow.rootViewController = navVC;
            });
        }else {
            [IndicatorManager showFailure:MESSAGE];
        }
    } failBloc:^(NSString * _Nonnull message) {
        [IndicatorManager showMessage:message];
    }];
}
@end
